<?php
/**
 * 用户管理
 * @author Ice <iceinto@mallog.com.cn>
 */
class UserAction extends CommonAction
{
    public function _initialize()
    {
        parent::_initialize();
        $this->assign('nav', 6);
    }

    /**
     * 用户默认列表
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function index()
    {
        $UserModel = D('User');
        $userCount = $UserModel->count();
        $Page = $this->getPage($userCount);
        $users = $UserModel->getList(null, $Page->firstRow . ',' . $Page->listRows);
        $this->assign('page', $Page->show());
        $this->assign('users', $users);
        $this->display();
    }

    /**
     * 更新用户信息
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function update()
    {
        $id = intval(getGet('id'));
        if ($id) {
            $this->getUser($id);
            //数据提交
            if (isPost()) {
                $UserModel = D('User');
                $data = $UserModel->create();
                if ($data) {
                    $data['id'] = $id;
                    if ($UserModel->save($data)) {
                        $this->getUser($id);
                        $this->successMessage(L('_UPDATE_SUCCESS'));
                    } else {
                        $this->error(L('_UPDATE_LOSE'));
                    }
                } else {
                    $this->error($UserModel->getError());
                }
            }
        } else {
            $this->error(L('_USER_ID_ERROR'));
        }
        $this->display();
    }

    /**
     * 添加用户信息
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function add()
    {
        if (isPost()) {
            $this->autoCheckToken();
            $UserModel = D('User');
            $data = $UserModel->create();
            if ($data) {
                $passwd = hh(getPost('passwd'));
                if ($passwd) {
                    $data['passwd'] = md5($passwd);
                } else {
                    $this->error('_PASSWD_MUST');
                }
                $data['fid'] = $this->getLoginUserInfo();
                if ($UserModel->add($data)) {
                    $this->successMessage(L('_ADD_SUCCESS'));
                } else {
                    $this->error(L('_ADD_LOSE'));
                }
            } else {
                $this->error($UserModel->getError());
            }
        }
        $this->display();
    }

    /**
     * Modify user password
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function passwd()
    {
        $id = intval(getGet('id'));
        if ($id) {
            $user = $this->getUser($id);
            if (isPost()) {
                $passwd = hh(getPost('passwd'));
                $repasswd = hh(getPost('repasswd'));
                if ($passwd && $repasswd) {
                    if ($passwd == $repasswd) {
                        $data = array();
                        $data['passwd'] = md5($passwd);
                        $data['id'] = $user['id'];
                        if (M('User')->save($data)) {
                            $this->successLang(L('_UPDATE_SUCCESS'));
                        } else {
                            $this->errorLang('_PASSWD_UPDATE_LOSE');
                        }
                    } else {
                        $this->errorLang('_PASSWD_AND_REPASSWD_MUST_EQUAL');
                    }
                } else {
                    $this->errorLang('_PASSWD_AND_REPASSWD_NO_EMPTY');
                }
            }
        } else {
            $this->errorLang('_USER_ID_ERROR');
        }
        $this->display();
    }

    /**
     * Get user
     * @param int $id user id
     * @author Ice <iceinto@mallog.com.cn>
     */
    private function getUser($id)
    {
        $user = M('User')->find($id);
        if ($user) {
            $this->assign('user', $user);
            return $user;
        } else {
            $this->errorLang('_USER_ID_ERROR');
        }
    }
}

?>